package com.foodost.cwpstrokes;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import net.tsz.afinal.core.Arrays;
import android.content.Context;
import android.content.SharedPreferences;


public class Utils {
    
  //获取整个成语的汉语拼音
    public static String getPinyin(String hanzi){
        
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); 

     // UPPERCASE：大写  (ZHONG)
     // LOWERCASE：小写  (zhong)
     format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

     // WITHOUT_TONE：无音标  (zhong)
     // WITH_TONE_NUMBER：1-4数字表示英标  (zhong4)
     // WITH_TONE_MARK：直接用音标符（必须WITH_U_UNICODE否则异常）  (zhòng)
     format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);

     // WITH_V：用v表示ü  (nv)
     // WITH_U_AND_COLON：用"u:"表示ü  (nu:)
     // WITH_U_UNICODE：直接用ü (nü)
     format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
     int length = hanzi.toCharArray().length;
     String[][] pinyin = new String[length][10];
     StringBuilder sb = new StringBuilder();
     try {
         for(int i = 0; i< length; i++){
             pinyin[i] = PinyinHelper.toHanyuPinyinStringArray(hanzi.charAt(i), format);
             if(pinyin[i] !=null && pinyin[i].length > 0){
            	 sb.append(pinyin[i][0] + " ");
             }else{
            	 sb.append("null");
             }
         }
        
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
     return sb.toString();
        
    }
    
    public static String unicodeToString(String str) {
        
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");    
 
        Matcher matcher = pattern.matcher(str);
 
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");    
 
        }
        return str;
 
    }
    
    public static void cwpstrokesInitFilish(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putBoolean(Constants.DATA_INITED, true);
        editor.apply();
    }
    
    public static boolean isCwpstrokesInitFilish(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        boolean inited = sf.getBoolean(Constants.DATA_INITED, false);
        return inited;
    }
    
    public static void setOrmGrage(Context context,int grage){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putInt(Constants.ORM_GRAGE, grage);
        editor.apply();
    }
    
    public static int getOrmGrage(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        int ormGrage = sf.getInt(Constants.ORM_GRAGE, 0);
        return ormGrage;
    }
    public static void setTimeStartIndex(Context context,int timeIndex){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putInt(Constants.TIME_START_INDEX, timeIndex);
        editor.apply();
    }
    
    public static int getTimeStartIndex(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        int timeIndex = sf.getInt(Constants.TIME_START_INDEX, 4);
        return timeIndex;
    }
    
    public static boolean getInitShortCut(Context context){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        boolean shortcutInited = sf.getBoolean(Constants.SHORTCUT_INITED, false);
        return shortcutInited;
    }
    
    public static void setInitShortCut(Context context,boolean inited){
        SharedPreferences sf = context.getSharedPreferences(Constants.DATA_PREF, 0);
        SharedPreferences.Editor editor = sf.edit();
        editor.putBoolean(Constants.SHORTCUT_INITED, inited);
        editor.apply();
    }
    
    public static List<String> StringToList(String hanziString){
        String[] hanziArr = hanziString.trim().substring(0, hanziString.length()-1).split(",");
        return Arrays.asList(hanziArr);
        
    }

}
